
void cod_ami (USER *pUsr, int *out) {
	int *b, i, j, k, x;
	for (b=pUsr->bits, i=k=0; i < pUsr->nbits; i++) {
    	if (b[i] == 0){
			for (j=0; j < pUsr->lSymb; j++,k++) {
	    		out[k] = 0;
			}
		} else {
			x = pUsr->V * pUsr->state.sgnOne;
		 	pUsr->state.sgnOne = -pUsr->state.sgnOne;
			for (j=0;j< pUsr->lSymb/2; j++,k++) {
		    	out[k] = x;
			}
			for (;j< pUsr->lSymb;j++, k++) {
		 		out[k] = 0;
	     	}
		}
	}
}

void cod_cmi (USER *pUsr, int *out) {
	int *b, i, j, k, x;
	for (b=pUsr->bits, i=k=0; i < pUsr->nbits; i++) {
    	if (b[i] == 0){
			for (j=0;j< pUsr->lSymb/2; j++,k++) {
		    	out[k] = -pUsr->V;
			}
			for (j=0;j< pUsr->lSymb/2;j++, k++) {
		 		out[k] = pUsr->V;
	     	}
		} else {
			x = pUsr->V * pUsr->state.sgnOne;
		 	pUsr->state.sgnOne = -pUsr->state.sgnOne;
			for (j=0;j< pUsr->lSymb; j++,k++) {
		    	out[k] = x;
			}
		}
	}
}


void cod_hdb3 (USER *pUsr, int *out) {
	int *b, i, j;
	b=pUsr->bits;
	for (i=0; i<pUsr->nbits; i++) {
		if (b[i]==1) {
			b[i] = pUsr->state.sgnOne;
			pUsr->state.sgnOne = -pUsr->state.sgnOne;
			pUsr->state.dc += b[i];
			pUsr->state.zeroes = 0;
		} else {
			pUsr->state.zeroes++;
			if (pUsr->state.zeroes == 4) {
				if (pUsr->state.dc) {
					b[i] = b[i-3] = pUsr->state.sgnOne;
					pUsr->state.sgnOne = -pUsr->state.sgnOne;
					pUsr->state.dc+=b[i];
					pUsr->state.dc+=b[i-3];
				} else {
					b[i] = -pUsr->state.sgnOne;
					pUsr->state.dc+=b[i];
				}
				pUsr->state.zeroes = 0;
			} 
		}
	}
	pUsr->bits = &(pUsr->bits[-3]);
	cod_rz(pUsr, out, pUsr->lSymb);
	pUsr->bits = b;
	for (i= -3, j = pUsr->nbits-3; i<0; i++, j++)
		b[i] = b[j];
}


void cod_manchester (USER *pUsr, int *out) {
	int *b, i, j, k, x;
	for (b=pUsr->bits, i=k=0; i < pUsr->nbits; i++) {
		x = ( b[i]==0 ? pUsr->V : -pUsr->V);
		for (j=0;j< pUsr->lSymb/2; j++,k++) {
	    	out[k] = x;
		}
		for (;j< pUsr->lSymb;j++, k++) {
	 		out[k] = -x;
     	}
	}
}

void cod_dif (USER *pUsr) {
	int i;
	for (i=0;i < pUsr->nbits; i++) {
		if (pUsr->state.lastBit==pUsr->bits[i]){
			pUsr->bits[i]=0;
		} else {
			pUsr->state.lastBit=pUsr->bits[i];
			pUsr->bits[i]=1;
		}
	}	
}
